/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-07-26
*/
package com.rzico.order.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.entity.Filter;
import com.rzico.entity.Pageable;
import com.rzico.entity.PageResult;
import com.rzico.order.entity.*;
import com.rzico.order.service.EmptyBarrelService;
import com.rzico.order.service.ReturnService;
import com.rzico.order.service.ShippingService;
import com.rzico.order.service.VirtualStockService;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.Api;
import com.rzico.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.Date;

/**
 * 空桶记录表控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-07-26
 */
@Api(description = "空桶记录表接口")
@RestController
@RequestMapping("/admin/emptyBarrel")
public class EmptyBarrelAdminController extends BaseController {

    @Autowired
    private EmptyBarrelService emptyBarrelService;

    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    private VirtualStockService virtualStockService;

    /**
     * 分页查询空桶记录表
     *
     * @return
    */
    @ApiOperation("分页查询空桶记录表")
    @GetMapping("/list")
    @ApiImplicitParams({@ApiImplicitParam(name = "startDate", value = "开始时间", dataType = "String", paramType = "query") ,
            @ApiImplicitParam(name = "endDate", value = "结束时间", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "String", paramType = "query")
    })
    public CommResult<EmptyBarrel> list(Date startDate, Date endDate,Long memberId, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);
        if (startDate!=null){
           params.put("startDate", startDate);
        }
        if (endDate!=null){
           params.put("endDate", DateUtils.addMilliseconds(DateUtils.addDays(endDate,1),-1));
        }

        if (memberId!=null){
            params.put("memberId", memberId);
        }

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }


        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<EmptyBarrel> list = emptyBarrelService.selectList(params);
        PageResult<EmptyBarrel> pageResult = new PageResult<EmptyBarrel>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 回桶登记
     *
     * @param emptyItemList
     * @return
     */
    @ApiOperation("回桶登记")
    @PostMapping("/addEmptyBarrel")
    public CommResult<EmptyBarrel> addEmptyBarrel(@RequestBody List<EmptyBarrel> emptyItemList) {

        Enterprise enterprise = enterpriseService.getCurrent();
        for (EmptyBarrel emptyBarrel:emptyItemList) {
            emptyBarrel.setEnterpriseId(enterprise.getId());
            emptyBarrel.setCreateDate(new Date());
        }
        emptyBarrelService.addEmptyBarrel(emptyItemList);

        return CommResult.success();

    }

    /**
     * 出桶登记
     *
     * @param emptyItemList
     * @return
     */
    @ApiOperation("出桶登记")
    @PostMapping("/decEmptyBarrel")
    public CommResult<EmptyBarrel> decEmptyBarrel(@RequestBody List<EmptyBarrel> emptyItemList) {
        Enterprise enterprise = enterpriseService.getCurrent();
        for (EmptyBarrel emptyBarrel:emptyItemList) {
            emptyBarrel.setEnterpriseId(enterprise.getId());
            emptyBarrel.setCreateDate(new Date());
        }
        emptyBarrelService.decEmptyBarrel(emptyItemList);

        return CommResult.success();

    }


    /**
     * 空桶压金汇总
     *
     * @return
     */
    @ApiOperation("空桶压金汇总")
    @PostMapping("/summary")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "String", paramType = "query")
    })
    public CommResult<Order> summary(Long memberId) {

        Enterprise enterprise = enterpriseService.getCurrent();

        Map<String,Object> data = new HashMap<>();

        //合计余桶
        data.put("emptyBarrel",emptyBarrelService.selectQuantitySum(memberId));

        //合计压金
        data.put("price",virtualStockService.selectPriceSum(enterprise.getId(),memberId,3));

        //合计压桶
        data.put("stock",virtualStockService.selectStockSum(enterprise.getId(),memberId,3));

        //存桶数
        data.put("emptyBarrelMany",0);
        //欠桶数
        data.put("emptyBarrelLess",0);


        return CommResult.success(data);

    }

}
